package com.banyou.banyou.order.pay;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.banyou.banyou.R;
import com.banyou.banyou.base.BaseActivity;
import com.banyou.banyou.bean.order.PayInfo;
import com.banyou.banyou.util.MyActivityManager;
import com.banyou.banyou.util.ShowToast;
import com.banyou.banyou.util.StringUtil;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.toolsfinal.StringUtils;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 类描述：
 * 创建人：Random
 * 修改时间：
 * 修改备注：
 */

public class PayActivity extends BaseActivity<PayPresenter> implements PayContract.View {

    @BindView(R.id.back_btn)
    ImageView mBackBtn;
    @BindView(R.id.price)
    TextView mPrice;
    @BindView(R.id.weixin_pay)
    RadioButton mWeixinPay;
    @BindView(R.id.zhifubao_pay)
    RadioButton mZhifubaoPay;
    @BindView(R.id.yinlian_pay)
    RadioButton mYinlianPay;
    @BindView(R.id.edit_yinlian)
    EditText mEditYinlian;
    @BindView(R.id.pay_btn)
    Button mPayBtn;

    private int mPayType = 0;
    private PayInfo mPayInfo = new PayInfo();

    @Override
    protected PayPresenter initPresenter() {
        return new PayPresenter();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_pay;
    }

    @Override
    protected View initLayoutView() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter.initPresenter(this, new PayModel());
        ButterKnife.bind(this);
        mEditYinlian.setVisibility(View.GONE);
        initTimeCount();
    }

    private static int count = 1800;
    private Handler mHandler = new Handler();
    private static Thread mThread;
    private static Object mObject = new Object();
    private static boolean isOnlyOneThread = true;

    /**
     * 开始倒计时
     */
    private void initTimeCount() {
        if (mThread != null) {
            isOnlyOneThread = false;
        }
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (mObject) {
                    if (!isOnlyOneThread) {
                        try {
                            mObject.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    while (count > 0 && isOnlyOneThread) {
                        synchronized (PayActivity.class) {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (PayActivity.this == MyActivityManager.getInstance().getLastActivity()) {
                                        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
                                        mPrice.setText("请在" + format.format(count * 1000) + "内支付");
                                    }
                                }
                            });
                            count--;
                        }
                    }
                    isOnlyOneThread = true;
                    mObject.notify();
                }
            }
        });
        mThread.start();
    }

    @Override
    protected String pageName() {
        return "支付页";
    }


    @OnClick({R.id.back_btn, R.id.weixin_pay, R.id.zhifubao_pay, R.id.yinlian_pay, R.id.pay_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                this.finish();
                break;
            case R.id.weixin_pay:
                mPayType = 3;
                mEditYinlian.setVisibility(View.GONE);
                break;
            case R.id.zhifubao_pay:
                mPayType = 4;
                mEditYinlian.setVisibility(View.GONE);
                break;
            case R.id.yinlian_pay:
                mPayType = 1;
                mEditYinlian.setVisibility(View.VISIBLE);
                break;
            case R.id.pay_btn:
                pay();
                break;
        }
    }

    /**
     * 支付逻辑
     */

    private void pay() {
        if (mPayType == 0) {
            ShowToast.showToast("请选择支付方式");
            return;
        } else if (mPayType == 1 && mEditYinlian.getText().toString().trim().length() == 0) {
            ShowToast.showToast("请输入银行卡号");
            return;
        } else if (mPayType == 1 && !StringUtil.isYinLian(mEditYinlian.getText().toString().trim())) {
            ShowToast.showToast("请输入正确的银行卡号");
            return;
        }
        mPayInfo.setOrderId(getIntent().getLongExtra("orderId", 0));
        mPayInfo.setPayBalance(getIntent().getIntExtra("price",0));
        mPayInfo.setPayType(mPayType + "");
        mPayInfo.setUserId(Integer.parseInt(getSharedPreference().getString("userId","")));
        mPresenter.Pay(JSON.toJSONString(mPayInfo));

    }

    /**
     * 支付成功
     */
    @Override
    public void PaySuccess() {
        ShowToast.showToast("正在跳转页面");
    }

    /**
     * 支付失败
     *
     * @param e
     */
    @Override
    public void PayFail(Throwable e) {

    }
}
